﻿@page "/diagramcomponent/shapes"

@using Syncfusion.Blazor.Diagram
@using System.Collections.ObjectModel
@inherits SampleBaseComponent;

<SampleDescription>
    <p>  This sample illustrates basic built-in shapes, such as basic shapes and flow shapes.</p>
</SampleDescription>
<ActionDescription>
    <p>This example shows how to define built-in shapes that are used to visualize geometric information, workflow, or business flow diagrams. The <code class="language-text">Type</code> property of the `Shape` allows you to choose the type of shape.</p>
</ActionDescription>
<SfDiagramComponent Height="790px" Nodes="@NodeCollection">
    <SnapSettings Constraints=SnapConstraints.None>
    </SnapSettings>
</SfDiagramComponent>

@code{


    //Defines diagrams's nodes collection
    DiagramObjectCollection<Node> NodeCollection = new DiagramObjectCollection<Node>();


    protected override void OnInitialized()
    {
        NodeCollection = new DiagramObjectCollection<Node>();

        #region Basic Shapes
        AddShapeText("Basic Shapes", NodeConstraints.PointerEvents);
        AddBasicShapes("rectLabel", "Rectangle", BasicShapes.Rectangle, false);
        AddBasicShapes("ellipseLabel", "Ellipse", BasicShapes.Ellipse, true);
        AddBasicShapes("triLabel", "Triangle", BasicShapes.Triangle, true);
        AddBasicShapes("plusLabel", "Plus", BasicShapes.Plus, true);
        AddBasicShapes("starLabel", "Star", BasicShapes.Star, true);
        AddBasicShapes("pentLabel", "Pentagon", BasicShapes.Pentagon, true);
        AddBasicShapes("heptLabel", "Heptagon", BasicShapes.Heptagon, true);
        AddBasicShapes("octLabel", "Octagon", BasicShapes.Octagon, true);
        AddBasicShapes("trapLabel", "Trapezoid", BasicShapes.Trapezoid, false);
        AddBasicShapes("decLabel", "Decagon", BasicShapes.Decagon, true);
        AddBasicShapes("rightLabel", "Right Triangle", BasicShapes.RightTriangle, true);
        AddBasicShapes("paraLabel", "Parallelogram", BasicShapes.Parallelogram, true);
        #endregion

        #region FlowShapes
        AddShapeText("Flow Shapes", NodeConstraints.PointerEvents);
        AddFlowShapes("terminateLabel", "Terminator", FlowShapes.Terminator);
        AddFlowShapes("processLabel", "Process", FlowShapes.Process);
        AddFlowShapes("decisionLabel", "Decision", FlowShapes.Decision);
        AddFlowShapes("documentLabel", "Document", FlowShapes.Document);
        AddFlowShapes("preLabel", "Predefined Process", FlowShapes.PreDefinedProcess);
        AddFlowShapes("directLabel", "Direct Data", FlowShapes.DirectData);
        AddFlowShapes("seqLabel", "Sequential Data", FlowShapes.SequentialData);
        AddFlowShapes("sortLabel", "Sort", FlowShapes.Sort);
        AddFlowShapes("multiLabel", "Multidocument", FlowShapes.MultiDocument);
        AddFlowShapes("collateLabel", "Collate", FlowShapes.Collate);
        AddFlowShapes("summingLabel", "Summing Junction", FlowShapes.SummingJunction);
        AddFlowShapes("or", "Or", FlowShapes.Or);
        AddFlowShapes("internalLabel", "Internal Storage", FlowShapes.InternalStorage);
        AddFlowShapes("extractLabel", "Extract", FlowShapes.Extract);
        AddFlowShapes("manualLabel", "Manual Operation", FlowShapes.ManualOperation);
        AddFlowShapes("mergeLabel", "Merge", FlowShapes.Merge);
        AddFlowShapes("offLabel", "Off-page Reference", FlowShapes.OffPageReference);
        AddFlowShapes("seqAccess", "Sequential Access Storage", FlowShapes.SequentialAccessStorage);
        AddFlowShapes("dataLabel", "Data", FlowShapes.Data);
        AddFlowShapes("cardLabel", "Card", FlowShapes.Card);
        AddFlowShapes("Preparation", "Preparation", FlowShapes.Preparation);
        #endregion

        #region BpmnShapes
        AddShapeText("BPMN Shapes", NodeConstraints.PointerEvents);
        Node startEventNode = new Node()
        {
            Shape = new BpmnShape()
            {
                Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn,
                Shape = BpmnShapes.Event,
                Events = new BpmnSubEvent() { Event = BpmnEvents.Start, Trigger = BpmnTriggers.None }
            },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>() {
        new ShapeAnnotation{ Content = "Start Event", ID = "startLabel" }
        },
        };
        NodeCollection.Add(startEventNode);

        Node interEventNode = new Node()
        {
            Shape = new BpmnShape()
            {
                Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn,
                Shape = BpmnShapes.Event,
                Events = new BpmnSubEvent() { Event = BpmnEvents.Intermediate, Trigger = BpmnTriggers.None }
            },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>() {
        new ShapeAnnotation{ Content = "Intermediate Event", ID = "interLabel" }
        },
        };
        NodeCollection.Add(interEventNode);

        Node endEventNode = new Node()
        {
            Shape = new BpmnShape()
            {
                Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn,
                Shape = BpmnShapes.Event,
                Events = new BpmnSubEvent() { Event = BpmnEvents.End, Trigger = BpmnTriggers.None }
            },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>() {
        new ShapeAnnotation{ Content = "End Event", ID = "endLabel" }
        },
        };
        NodeCollection.Add(endEventNode);
        Node node5 = new Node()
        {
            ID = "node5",

            Width = 70,
            Height = 70,
            Annotations = new DiagramObjectCollection<ShapeAnnotation>()
        {
                new ShapeAnnotation
                {
                    Content="Task"
                }
            },
            Shape = new BpmnShape() { Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn, Shape = BpmnShapes.Activity, Activity = new BpmnActivity() { Activity = BpmnActivities.Task, Task = new BpmnTask() { Type = BpmnTasks.BusinessRule } } }
        };
        NodeCollection.Add(node5);
        Node node1 = new Node()
        {
            ID = "node1",

            Width = 70,
            Height = 70,
            Annotations = new DiagramObjectCollection<ShapeAnnotation>()
        {
                new ShapeAnnotation
                {
                    Content="SubProcess"
                }
            },
            Shape = new BpmnShape() { Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn, Shape = BpmnShapes.Activity, Activity = new BpmnActivity() { Activity = BpmnActivities.SubProcess, SubProcess = new BpmnSubProcess { Collapsed = true } } }
        };
        NodeCollection.Add(node1);
        AddBpmnShape("messageLabel", "Message", BpmnShapes.Message);
        AddBpmnShape("dataObjectLabel", "Data Object", BpmnShapes.DataObject);
        AddBpmnShape("dataSourceLabel", "Data Source", BpmnShapes.DataSource);
        AddBpmnShape("gatewayLabel", "Gateway", BpmnShapes.Gateway);
        #endregion

        UpdateValues();
    }
    private void AddBpmnShape(string id, string shapeText, BpmnShapes bpmn)
    {
        Node startEventNode = new Node()
        {
            Shape = new BpmnShape()
            {
                Type = Syncfusion.Blazor.Diagram.Shapes.Bpmn,
                Shape = bpmn,
            },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>()
       {
             new ShapeAnnotation
            {
               Content = shapeText, ID = id
             }
          }
        };

        NodeCollection.Add(startEventNode);
    }
    private void AddShapeText(string textContent, NodeConstraints constraints)
    {
        Node node = new Node()
        {
            Annotations = new DiagramObjectCollection<ShapeAnnotation>()
        {
                        new ShapeAnnotation
                        {
                            Content = textContent,
                        }
            },
            Style = new TextShapeStyle() { FontSize = 16, Fill = "None", FontFamily = "sans-serif", Bold = true, StrokeWidth = 0 },
            Constraints = constraints
        };
        NodeCollection.Add(node);
    }
    private void AddBasicShapes(string id, string shapeText, BasicShapes shapes, bool CheckAspectRatio)
    {
        Node node = new Node()
        {

            Shape = new BasicShape() { Type = Syncfusion.Blazor.Diagram.Shapes.Basic, Shape = shapes },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>()
        {
                        new ShapeAnnotation
                        {
                            Content = shapeText,
                            ID = id,
                        }
            },

        };
        if (CheckAspectRatio)
        {
            node.Constraints = node.Constraints | NodeConstraints.AspectRatio;
        }
        NodeCollection.Add(node);
    }
    private void UpdateValues()
    {
        int offsetx = 60;
        int offsety = 50;
        int count = 1;

        for (int i = 0; i < NodeCollection.Count; i++)
        {
            Node node = NodeCollection[i];
            node.Width = 40;
            node.Height = 40;

            if (node.Shape.Type == Syncfusion.Blazor.Diagram.Shapes.Flow)
            {

                if (node.Shape is FlowShape)
                {

                    FlowShape shapeType = node.Shape as FlowShape;
                    if (shapeType.Shape == FlowShapes.Process || shapeType.Shape == FlowShapes.Terminator)
                    {
                        node.Height = 20;
                    }
                    else if (shapeType.Shape == FlowShapes.Decision)
                    {
                        node.Height = 35;
                    }
                    else if (shapeType.Shape == FlowShapes.Document || shapeType.Shape == FlowShapes.DirectData ||
                      shapeType.Shape == FlowShapes.MultiDocument || shapeType.Shape == FlowShapes.PreDefinedProcess)
                    {
                        node.Height = 30;
                    }
                }
            }

            if (node.Annotations[0].Content != "Basic Shapes" && node.Annotations[0].Content != "Flow Shapes" && node.Annotations[0].Content != "BPMN Shapes")
            {
                if (node.Annotations.Count > 0)
                {
                    node.Annotations[0].VerticalAlignment = VerticalAlignment.Bottom;
                    node.Annotations[0].Offset = new Point() { X = 0.5, Y = 1 };
                    node.Annotations[0].Margin = new Margin() { Top = 10 };
                }
                node.OffsetX = offsetx;
                node.OffsetY = offsety;
                offsetx = offsetx + 90;
                if (count % 10 == 0)
                {
                    offsety = offsety + 100;
                    offsetx = 60;
                }
                count++;
            }

            if (node.Annotations[0].Content == "Basic Shapes" || node.Annotations[0].Content == "Flow Shapes" || node.Annotations[0].Content == "BPMN Shapes")
            {
                node.Annotations[0].Style.Bold = true;
                node.Annotations[0].Style.FontSize = 16;

                offsetx = 60;
                node.OffsetX = offsetx;

                if (count > 1)
                {
                    offsety += 100;
                }
                node.OffsetY = offsety;
                offsety = offsety + 50;
                count = 1;
                node.Width = 150;
                node.Height = 50;
                node.OffsetX = 90;
            }
        }
    }

    private void AddFlowShapes(string id, string shapeText, FlowShapes shapes)
    {
        Node terminatNode = new Node()
        {
            Shape = new FlowShape() { Type = Syncfusion.Blazor.Diagram.Shapes.Flow, Shape = shapes },
            Annotations = new DiagramObjectCollection<ShapeAnnotation>() {
            new ShapeAnnotation {
                Content = shapeText,ID = id }
            }
        };
        NodeCollection.Add(terminatNode);
    }
}

